<?php
/**
 * Author:   tangxiaowen
 * Email:    846506584@qq.com
 * Date:     2019/5/22 0022
 * Describe: 用户信息模型类.
 */

namespace app\common\model;

use think\Model;

class User extends Model
{
    //错误信息
    public $error = 'error';

    /**
     * 获取用户信息(立时贷用).
     *
     * @param array  $postData 匹配条件
     * @param string $field    需要字段|默认所有字段
     *
     * @return array
     */
    public function getUserInfo($postData, $field = '*')
    {
        $where['TrueName2'] = $postData['user_name'];
        $where['Mobile'] = ['like', substr($postData['user_phone'], 0, 7).'%'];
        $where['IDCard'] = ['like', substr($postData['user_idcard'], 0, 14).'%'];

        return db('mem_info')->where($where)->field($field)->find();
    }

    /**
     * 根据ID获取用户信息.
     *
     * @param string $param 用户ID|手机号
     * @param array  $field 需要的字段
     *
     * @return array
     */
    public function getIdUserInfo($param, $field = [])
    {
        return db('mem_info')->where('ID|Mobile', $param)->field($field)->find();
    }

    /**
     * 检测用户状态
     *
     * @param array $userInfo 用户信息
     *
     * @return bool
     */
    public function isUserStatus($userInfo)
    {
        if ($userInfo['Status'] == 1) {
//          $this->error = '用户信息待审核';
//
//          return false;
             return true;
        } elseif ($userInfo['Status'] == 2) {
            return true;
        } elseif ($userInfo['Status'] == 3) {
            $this->error = '用户禁用状态';

            return false;
        } elseif ($userInfo['Status'] == 4) {
            $this->error = '黑名单用户';

            return false;
        } else {
            return false;
        }
    }

    /**
     * 检测用户是否符合借款年龄范围.
     *
     * @param array $userInfo 用户信息
     *
     * @return bool
     */
    public function isUserAge($userInfo)
    {
        $userAge = substr($userInfo['IDCard'], 6, 4);
        $age = date('Y') - $userAge;
        $systemInfo = (new System())->getSystemInfo();
        if ($age > $systemInfo['MinAges'] && $age < $systemInfo['MaxAges']) {
            return true;
        }
        $this->error = '年龄不符合范围';

        return false;
    }

    /**
     * 获取用户进行中的订单.
     *
     * @param int $userId 用户ID
     *
     * @return array
     */
    public function getUserOrder($userId)
    {
        $where['UserID'] = $userId;
        $where['LoanStatus'] = ['in', '0,1,2,6,7'];

        return db('loans_applylist')->where($where)->find();
    }

    /**
     * 检测用户是否有逾期订单.
     *
     * @param int $userId 用户ID
     *
     * @return array
     */
    public function getUserOverdueOrder()
    {
        $where['LoanStatus'] = 2;

        return db('loans_applylist')->where($where)->where('YyFkTime', '< time', date('Y-m-d'))->find();
    }

    /**
     * 检测用户距离上笔被拒订单还剩余多久可再次申请提示.
     *
     * @param int $userId 用户ID
     *
     * @return bool|dateTime
     */
    public function isUserRefuseOrder($userId)
    {
        $order = db('loans_applylist')->where(['UserID' => $userId, 'LoanStatus' => 5])->order('id', 'desc')->find();
        if ($order) {
            $days = (int) db('mem_info')->where(['ID' => $userId])->value('refuseDay');
            $refuseTime = date('Y-m-d H:i:s', strtotime($order['ShTime']) + ($days * 86400));
            if (strtotime($refuseTime) > time()) {
                return date('Y-m-d', strtotime($refuseTime));
            } else {
                return false;
            }
        }

        return false;
    }

    /**
     * 获取用户可借款最大金额与最小金额.
     *
     * @param int $userId 用户ID
     *
     * @return array|false
     */
    public function getLoanMoney($userId)
    {
        //获取用户陈功借款次数
        $loanNum = db('loans_applylist')->where(['UserID' => $userId, 'LoanStatus' => 3])->count('ID');
        $loanNum = ($loanNum) ? $loanNum : 1;

        //根据成功借款次数获取可借款产品信息
        $where['t.num'] = ['<=', $loanNum];
        $where['a.Status'] = 1; //正常客户展期开关 1 开启 0关闭
        $where['a.IsShelves'] = 1; //是否上架: 0 未上架 1 已上架
        $money = db('goods')->alias('a')
            ->join('xb_sys_tier t', 'a.ID = t.money', 'left')
            ->group('a.ID')
            ->where($where)
            ->select();

        if (empty($money)) {
            $this->error = '无可借贷产品';

            return false;
        }

        $temp = [];
        foreach ($money as $k => $v) {
            $temp[$k] = $v['SalePrice'];
        }

        return $temp;
    }

    /**
     * 获取用户绑卡信息.
     *
     * @param int $userId 用户ID
     *
     * @return array
     */
    public function getUserBank($userId)
    {
        return db('renzen_bank')->where(['UserID' => $userId, 'Status' => 1])->find();
    }

    /**
     * 获取用户与银行卡信息.
     *
     * @param int $userId 用户ID
     *
     * @return array
     *
     *  立时贷【绑卡结果状态】状态码
     *  100	需短信验证码，请输入已发送的验证码重新绑卡
     *  200	绑卡成功
     *  401	认证信息不匹配
     *  402	卡号对应的银行不正确
     *  403	无法验证，银行卡未开通认证支付，请到开户行开户
     *  501	绑卡失败当天次数超限，请24小时后重试！
     *  505	其它绑卡失败原因，请备注用户前端透传
     */
    public function getUserAndBank($userId)
    {
        $temp = [];
        $data = db('mem_info')->alias('m')
            ->join('xb_renzen_bank b', 'm.ID = b.UserID', 'LEFT')
            ->where(['m.ID' => $userId])
            ->field('m.*,b.Status as bStatus,b.BankName,b.BankNo,b.YMobile')
            ->find();
        if ($data) {
            $temp = [
                'bind_status' => 200, //绑卡结果状态; 参考订单绑卡接口 状态码 (由于银行卡认证都是成功才入库 故此处写死200)
                'bank_name' => $data['BankName'], //绑卡银行名称
                'user_name' => $data['TrueName'], //用户姓名
                'user_idcard' => $data['IDCard'], //用户身份证号
                'card_number' => $data['BankNo'], //银行卡号
                'card_phone' => $data['YMobile'], //银行预留手机号
                'card_type' => 2, //银行卡类型 1 信用卡 2 借记卡
                'user_phone' => $data['Mobile'], //用户准入时的明文手机号
                'use_type' => 0, //银行卡用途 0:既是收款卡,又是还款卡 1:收款卡 2:还款卡
            ];
        }

        return $temp;
    }

    /**
     * 获取用户社交信息
     * @param int $userId 用户ID
     * @return array
     */
    public function getUserSocial($userId)
    {
        return db('renzen_social')->where(['UserID' => $userId])->find();
    }

    /**
     * 获取用户魔蝎原始json数据
     * @param int $userId 用户ID
     * @return json
     */
    public function getMoXieData($userId)
    {
        return db('moxie_origin')->where(['UserID' => $userId,'Type' => 1])->value('OriginData');
    }

    /**
     * 获取用户魔蝎运营商报告信息
     * @param int $userId 用户ID
     * @return josn
     */
    public function getMoXieReport($userId)
    {
        return db('moxie_report')->where(['UserID' => $userId,'Type' => 1])->find();
    }

    /**
     * 获取用户魔蝎原始json数据
     * @param int $userId 用户ID
     * @return json
     */
    public function getXinYanData($userId)
    {
        return db('moxie_origin')->where(['UserID' => $userId,'Type' => 2])->value('OriginData');
    }

    /**
     * 获取用户魔蝎运营商报告信息
     * @param int $userId 用户ID
     * @return josn
     */
    public function getXinYanReport($userId)
    {
        return db('moxie_report')->where(['UserID' => $userId,'Type' => 2])->value('ReportData');
    }
    
    /**
     * 获取用户有盾数据
     * @param int $userId 用户ID
     * @return josn
     */
    public function getYouDunData($userId)
    {
        return db('renzen_cards')->where(['UserID' => $userId])->value('Yddatas');
    }

}
